Imports System.data
Partial Class Client_frmCreditCard
    Inherits System.Web.UI.Page
    Dim _UserId As String
    Dim _BillDate As String
    Dim _Date As String
    Dim i As Integer
    Dim _bo As New Bo
    Dim _ProductVo As New ProductVo
    Dim _CreditCardVo As New CreditCardVo
    Dim _ShippingChargeVo As New ShippingChargeVo
    Dim _BillDetailVo As New BillDetailVo
    Dim _BillMasterVo As New BillMasterVo
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _UserId = Session("UserId")
        If _UserId = "" Then
            Response.Redirect("frmLogin.aspx")
        End If
        If Not Page.IsPostBack Then
            For i = 1 To 12
                ddlMonth.Items().Add(i)
            Next
            For i = System.DateTime.Now.Year.ToString To (Val(System.DateTime.Now.Year.ToString) + 25)
                ddlYear.Items().Add(i)
            Next
        End If
    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim x As String = System.DateTime.Now.ToString
        'Dim i As Integer = x.IndexOf("/")
        'Dim j As Integer = x.LastIndexOf("/")
        'Dim s As String = x.Substring(0, i)
        'Dim t As String = x.Substring((j + 1), (j + 2))
        'If (((t.Equals(ddlYear.SelectedValue.ToString)) And (s <= ddlMonth.SelectedValue.ToString)) Or (t > ddlYear.SelectedValue.ToString)) Then

        If (ddlYear.SelectedValue.ToString > System.DateTime.Now.Year.ToString) Then
            Call InsertCreditCard()
            Call InsertBillMaster()
            Call InsertBillDetail()
            clearall()
            Session("UserID") = _UserId
            Response.Redirect("frmBillingAddress.aspx")
        ElseIf (ddlYear.SelectedValue.ToString = System.DateTime.Now.Year.ToString) Then
            If ((Val(ddlMonth.SelectedValue.ToString) >= Val(System.DateTime.Now.Month.ToString))) Then
                Call InsertCreditCard()
                Call InsertBillMaster()
                Call InsertBillDetail()
                clearall()
                Session("UserID") = _UserId
                Response.Redirect("frmBillingAddress.aspx")
            Else
                MsgBox("Your Card Has Been Expired.")
            End If
        End If

    End Sub
    Sub clearall()
        txtBankName.Text = ""
        txtCreditCardNumber.Text = ""
        txtCreditCardType.Text = ""
        txtCreditCardVerificationNumber.Text = ""
        txtHolderName.Text = ""
    End Sub
    Sub InsertBillMaster()
        Dim dt As New DataTable
        Dim dt1 As New DataTable
        Dim x As Integer
        Dim date1 As String
        Dim dt2 As New DataTable
        _BillMasterVo.UserId = _UserId
        date1 = System.DateTime.Now.ToShortDateString
        x = date1.LastIndexOf("/")
        x = x + 5
        ' _BillMasterVo.BillDate = date1.Substring(0, x)
        _BillMasterVo.BillDate = System.DateTime.Now

        '_BillMasterVo.BillDate = System.DateTime.Now.ToShortDateString
        dt = _bo.getTotal(_UserId)
        _BillMasterVo.TotalQuantity = dt.Rows(0).Item(0).ToString
        _BillMasterVo.Price = dt.Rows(0).Item(1).ToString
        dt1 = _bo.getBillNo()
        _Date = dt1.Rows(0).Item(0).ToString
        _Date = (Val(_Date) + 1)
        _bo.UpdateBillNo(_Date)

        _BillDate = System.DateTime.Now.Year & System.DateTime.Now.Month & System.DateTime.Now.Day & _Date
        _BillMasterVo.BillId = _BillDate
        dt2 = _bo.getShippingCharge("s1")
        _BillMasterVo.ShippingCharge = dt2.Rows(0).Item(1).ToString
        _BillMasterVo.CreditCardNumber = txtCreditCardNumber.Text
        _bo.insertBillMaster(_BillMasterVo)
    End Sub
    Sub InsertBillDetail()
        Dim dtCartDetail As New DataTable
        Dim _AvailableQuantity As String
        Dim _RemainingQuantity As String
        dtCartDetail = _bo.getCart(_UserId)
        For i = 0 To dtCartDetail.Rows.Count - 1
            _BillDetailVo.ProductId = dtCartDetail.Rows(i).Item("ProductId").ToString
            _BillDetailVo.BillId = _BillDate
            _BillDetailVo.Quantity = dtCartDetail.Rows(i).Item("Quantity").ToString
            _BillDetailVo.Price = dtCartDetail.Rows(i).Item("DiscountedPrice").ToString
            _BillDetailVo.Size = dtCartDetail.Rows(i).Item("Size").ToString
            _bo.insertBillDetail(_BillDetailVo)
            _ProductVo = _bo.getProductById(dtCartDetail.Rows(i).Item("ProductId").ToString)
            _AvailableQuantity = _ProductVo.AvailableQuantity
            _RemainingQuantity = (Integer.Parse(_AvailableQuantity) - Integer.Parse(dtCartDetail.Rows(i).Item("Quantity").ToString))
            _bo.DeductQuantity(dtCartDetail.Rows(i).Item("ProductId").ToString, _RemainingQuantity)
        Next
    End Sub
    Sub InsertCreditCard()
        _CreditCardVo.CreditCardVerificationNumber = txtCreditCardVerificationNumber.Text
        _CreditCardVo.CreditCardType = txtCreditCardType.Text
        _CreditCardVo.CreditCardNumber = txtCreditCardNumber.Text
        _CreditCardVo.BankName = txtBankName.Text
        _CreditCardVo.HolderName = txtHolderName.Text
        _CreditCardVo.ExpiryDate = (ddlMonth.SelectedValue.ToString & "/" & ddlYear.SelectedValue.ToString)
        _bo.insertCreditCard(_CreditCardVo)
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        clearall()
    End Sub
End Class
